import { useEffect } from 'react';
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

const { CustomPictureSelector } = NativeModules;

// 事件监听（如果使用事件方式）
const eventEmitter = new NativeEventEmitter(CustomPictureSelector);


export enum ImageSelectType {
    TYPE_ALL = 0,
    TYPE_IMAGE = 1,
    VIDEO = 2,
    AUDIO = 3,
}

interface MediaItem {
    bucketId: number;
    chooseModel: number;
    compressPath: string | null;
    compressed: boolean;
    cropImageHeight: number;
    cropImageWidth: number;
    cropOffsetX: number;
    cropOffsetY: number;
    cropResultAspectRatio: number;
    customData: string | null;
    cutPath: string | null;
    dateAddedTime: number;
    duration: number;
    fileName: string;
    height: number;
    id: number;
    isCameraSource: boolean;
    isChecked: boolean;
    isCut: boolean;
    isEditorImage: boolean;
    isGalleryEnabledMask: boolean;
    isMaxSelectEnabledMask: boolean;
    isOriginal: boolean;
    mimeType: string;
    num: number;
    originalPath: string | null;
    parentFolderName: string;
    path: string;
    position: number;
    realPath: string;
    sandboxPath: string | null;
    size: number;
    videoThumbnailPath: string | null;
    watermarkPath: string | null;
    width: number;
}

interface ImageSelectorOptions {
    maxSelectNum: number;
    /*
    * 0 全部
    * 1 图片
    * 2 视频
    * 3 音频
    * */
    imageSelectType: ImageSelectType;
    /** 是否使用自定义相机，是就用jsFinishTakePhoto+ event 'pictureSelector_selectCustomCamera' */
    isUseOnCustomerCamera: Boolean
}
export const openImageSelector= async (options :ImageSelectorOptions): Promise<MediaItem[]> => {
    if (Platform.OS !== 'android') {
        return Promise.reject(new Error('This module only supports Android'));
    }
    return CustomPictureSelector.openImageSelector(options);
}

const removeAllListeners=(eventName) => {
    if (Platform.OS !== 'android') return;
    return eventEmitter.removeAllListeners(eventName);
}

/** 如果用自定义拍照，就使用该函数回调拍好的照片 */
export const jsFinishTakePhoto = async(filePath:string)=>{
    return CustomPictureSelector.jsFinishTakePhoto({filePath});
}

let isStartCustomerCamera = false
export const addListenerUseCustomerCamera=(callback:(type:'picture'|'video', )=>any)=>{
    if (Platform.OS !== 'android') return;
    eventEmitter.addListener('pictureSelector_selectCustomCamera', ()=>{
        // 打开自定义js拍照相机
        console.log("event trigger pictureSelector_selectCustomCamera")
        isStartCustomerCamera = true
        callback('picture')
    });
    eventEmitter.addListener('pictureSelector_selectCustomVideoCamera', ()=>{
        // 打开自定义js录像
        console.log("event trigger pictureSelector_selectCustomVideoCamera")
        isStartCustomerCamera = true
        callback('video')
    });
}

export const removeListenerUseCustomerCamera=()=>{
    removeAllListeners('pictureSelector_selectCustomCamera')
    removeAllListeners('pictureSelector_selectCustomVideoCamera')
}

export const afterCloseCustomerCamera=()=>{
    if(!isStartCustomerCamera) return
    CustomPictureSelector.afterCloseCustomerCamera()
    isStartCustomerCamera = false
}
